/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 2010 Bull S.A.S.
 * Contact: jonas-team@ow2.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 *
 * --------------------------------------------------------------------------
 * $Id: AbstractRulesTestCase.java 53 2010-05-28 16:24:38Z guillaume.sauthier $
 * --------------------------------------------------------------------------
 */


package org.osami.gateway.rules.heating;

import java.net.URL;
import java.util.Set;

import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseConfiguration;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.conf.EventProcessingOption;
import org.drools.io.ResourceFactory;
import org.testng.annotations.BeforeTest;

/**
 * A {@code AbstractRulesTestCase} is ...
 *
 * @author Guillaume Sauthier
 */
public class AbstractRulesTestCase {

    protected KnowledgeBase buildKnowledgeBase(final Set<URL> resourceUrls) throws Exception {

        // Produce a Builder
        KnowledgeBuilder knowledgeBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();

        // Add Resources (the rules definition) inside
        for (URL resourceUrl : resourceUrls) {
            knowledgeBuilder.add(ResourceFactory.newUrlResource(resourceUrl),
                                 ResourceType.DRL);
        }

        if (knowledgeBuilder.hasErrors()) {
            throw new Exception("Cannot construct KnowledgePackages: " + knowledgeBuilder.getErrors());
        }

        // Construct a KnowledgeBase in stream mode ...
        KnowledgeBaseConfiguration configuration = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
        configuration.setOption(EventProcessingOption.STREAM);

        KnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase(configuration);
        // ... and configure it with the KnowledgePackages
        knowledgeBase.addKnowledgePackages(knowledgeBuilder.getKnowledgePackages());

        return knowledgeBase;
    }

}
